package com.ziwei.doushu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.ziwei.doushu.model.ZiweiResult;
import java.util.List;
import java.util.Map;

/**
 * 结果展示界面Activity
 */
public class ResultActivity extends AppCompatActivity {
    
    private TextView tvPalaceInfo;
    private TextView tvStarInfo;
    private TextView tvPatternInfo;
    private TextView tvFortuneInfo;
    private Button btnBack;
    private Button btnSave;
    
    private ZiweiResult result;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        
        initViews();
        setListeners();
        displayResult();
    }
    
    private void initViews() {
        tvPalaceInfo = findViewById(R.id.tv_palace_info);
        tvStarInfo = findViewById(R.id.tv_star_info);
        tvPatternInfo = findViewById(R.id.tv_pattern_info);
        tvFortuneInfo = findViewById(R.id.tv_fortune_info);
        btnBack = findViewById(R.id.btn_back);
        btnSave = findViewById(R.id.btn_save);
    }
    
    private void setListeners() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveResult();
            }
        });
    }
    
    private void displayResult() {
        // 获取结果数据
        Intent intent = getIntent();
        result = (ZiweiResult) intent.getSerializableExtra("result");
        
        if (result == null) {
            Toast.makeText(this, "结果数据异常", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        // 显示十二宫位信息
        displayPalaceInfo();
        
        // 显示主星分布
        displayStarInfo();
        
        // 显示格局分析
        displayPatternInfo();
        
        // 显示大运流年
        displayFortuneInfo();
    }
    
    private void displayPalaceInfo() {
        Map<String, String> palaceInfo = result.getPalaceInfo();
        if (palaceInfo != null) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : palaceInfo.entrySet()) {
                sb.append(entry.getKey()).append("：").append(entry.getValue()).append("\n\n");
            }
            tvPalaceInfo.setText(sb.toString());
        }
    }
    
    private void displayStarInfo() {
        Map<String, String> starInfo = result.getStarInfo();
        if (starInfo != null) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : starInfo.entrySet()) {
                sb.append(entry.getKey()).append("星：").append(entry.getValue()).append("\n");
            }
            tvStarInfo.setText(sb.toString());
        }
    }
    
    private void displayPatternInfo() {
        String patternAnalysis = result.getPatternAnalysis();
        if (patternAnalysis != null && !patternAnalysis.isEmpty()) {
            tvPatternInfo.setText(patternAnalysis);
        } else {
            tvPatternInfo.setText("格局分析：根据命盘分析，此命格具有独特的特点，需要结合具体宫位和主星进行详细解读。");
        }
    }
    
    private void displayFortuneInfo() {
        List<String> fortuneInfo = result.getFortuneInfo();
        if (fortuneInfo != null && !fortuneInfo.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (String fortune : fortuneInfo) {
                sb.append(fortune).append("\n");
            }
            tvFortuneInfo.setText(sb.toString());
        } else {
            tvFortuneInfo.setText("大运流年：根据命盘推算，各阶段运势如下：\n" +
                "5-14岁：早年运势平稳\n" +
                "15-24岁：青年运势上升\n" +
                "25-34岁：中年运势旺盛\n" +
                "35-44岁：事业运势强盛\n" +
                "45-54岁：财运亨通\n" +
                "55-64岁：晚年运势稳定");
        }
    }
    
    private void saveResult() {
        // 这里可以实现保存到数据库的功能
        Toast.makeText(this, "结果已保存", Toast.LENGTH_SHORT).show();
        
        // 可以添加数据库保存逻辑
        // DatabaseHelper dbHelper = new DatabaseHelper(this);
        // dbHelper.saveResult(result);
    }
}
